// 通用API服务
const ApiService = {
    // 通用CRUD操作
    async create(endpoint, data) {
        try {
            const response = await Utils.fetchData(Utils.getApiUrl(endpoint), {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify(data)
            });
            return response;
        } catch (error) {
            console.error('创建失败:', error);
            throw error;
        }
    },

    async update(endpoint, id, data) {
        try {
            const response = await Utils.fetchData(Utils.getApiUrl(`${endpoint}/${id}`), {
                method: 'PUT',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify(data)
            });
            return response;
        } catch (error) {
            console.error('更新失败:', error);
            throw error;
        }
    },

    async delete(endpoint, id) {
        try {
            const response = await Utils.fetchData(Utils.getApiUrl(`${endpoint}/${id}`), {
                method: 'DELETE'
            });
            return response;
        } catch (error) {
            console.error('删除失败:', error);
            throw error;
        }
    },

    async toggleStatus(endpoint, id, status) {
        try {
            const response = await Utils.fetchData(Utils.getApiUrl(`${endpoint}/${id}/status`), {
                method: 'PUT',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ status })
            });
            return response;
        } catch (error) {
            console.error('状态切换失败:', error);
            throw error;
        }
    },

    // 用户相关API
    users: {
        async getTeachers(page = 1, size = 10, search = '') {
            return await Utils.fetchData(Utils.getApiUrl(`/chm/api/users/teachers?page=${page}&size=${size}&search=${encodeURIComponent(search)}`));
        },
        async getStudents(page = 1, size = 10, search = '') {
            return await Utils.fetchData(Utils.getApiUrl(`/chm/api/users/students?page=${page}&size=${size}&search=${encodeURIComponent(search)}`));
        },
        async createUser(userData) {
            return await ApiService.create('/chm/api/users', userData);
        },
        async updateUser(id, userData) {
            return await ApiService.update('/chm/api/users', id, userData);
        },
        async deleteUser(id) {
            return await ApiService.delete('/chm/api/users', id);
        },
        async toggleUserStatus(id, status) {
            return await ApiService.toggleStatus('/chm/api/users', id, status);
        }
    },

    // 学科相关API
    subjects: {
        async getSubjects(page = 1, size = 10, search = '') {
            return await Utils.fetchData(Utils.getApiUrl(`/chm/api/subjects?page=${page}&size=${size}&search=${encodeURIComponent(search)}`));
        },
        async createSubject(subjectData) {
            return await ApiService.create('/chm/api/subjects', subjectData);
        },
        async updateSubject(id, subjectData) {
            return await ApiService.update('/chm/api/subjects', id, subjectData);
        },
        async deleteSubject(id) {
            return await ApiService.delete('/chm/api/subjects', id);
        },
        async toggleSubjectStatus(id, status) {
            return await ApiService.toggleStatus('/chm/api/subjects', id, status);
        }
    },

    // 成绩相关API
    scores: {
        async getScores(page = 1, size = 10, search = '') {
            return await Utils.fetchData(Utils.getApiUrl(`/chm/api/scores?page=${page}&size=${size}&search=${encodeURIComponent(search)}`));
        },
        async createScore(scoreData) {
            return await ApiService.create('/chm/api/scores', scoreData);
        },
        async updateScore(id, scoreData) {
            return await ApiService.update('/chm/api/scores', id, scoreData);
        },
        async deleteScore(id) {
            return await ApiService.delete('/chm/api/scores', id);
        }
    }
};

// 注册到全局作用域
window.ApiService = ApiService;
